﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Vds2465Telegrams
{
	public enum eTelegramFunction_Returning
	{
		CONFIRM_ACK,
		CONFIRM_NAK,
		FC_ERROR,
		RESPOND_NAK,
		RESPOND_STATUS,
		RESPOND_NDAT
	}

	public class Telegram_Returning : Telegram
	{
		#region private member variables

		private readonly eTelegramFunction_Returning _telegramFunction;

		/// <summary>
		/// set to true to signalize access demand for data of class 1
		/// </summary>
		private readonly bool _accessDemandFlag;

		/// <summary>
		/// set to true to signalize that no further messages can be processed
		/// </summary>
		private readonly bool _dataFlowControlStopFlag;

		#endregion private member variables

		#region constructor

		public Telegram_Returning(eDirection direction, byte address, eTelegramFunction_Returning telegramFunctionReturning, bool accessDemandFlag, bool dataFlowControlStopFlag) :
			base(direction, address)
		{
			_telegramFunction = telegramFunctionReturning;
			_accessDemandFlag = accessDemandFlag;
			_dataFlowControlStopFlag = dataFlowControlStopFlag;
		}

		#endregion constructor

		#region public getters

		public eTelegramFunction_Returning TelegramFunction { get { return _telegramFunction; } }

		/// <summary>
		/// set to true to signalize access demand for data of class 1
		/// </summary>
		public bool AccessDemandFlag { get { return _accessDemandFlag; } }

		/// <summary>
		/// set to true to signalize that no further messages can be processed
		/// </summary>
		public bool DataFlowControlStopFlag { get { return _dataFlowControlStopFlag; } }

		#endregion public getters

		#region ToString, Equals, GetHashCode

		public override int GetHashCode()
        {
            int hashCode = base.GetHashCode();
            if (AccessDemandFlag)
            {
                hashCode ^= 0x01;
            }
            if (DataFlowControlStopFlag)
            {
                hashCode ^= 0x02;
            }
            switch (TelegramFunction)
            {
                case eTelegramFunction_Returning.CONFIRM_ACK:
                    hashCode ^= 0x04;
                    break;
                case eTelegramFunction_Returning.CONFIRM_NAK:
                    hashCode ^= 0x08;
                    break;
                case eTelegramFunction_Returning.FC_ERROR:
                    hashCode ^= 0x10;
                    break;
                case eTelegramFunction_Returning.RESPOND_NAK:
                    hashCode ^= 0x20;
                    break;
                case eTelegramFunction_Returning.RESPOND_STATUS:
                    hashCode ^= 0x40;
                    break;
                case eTelegramFunction_Returning.RESPOND_NDAT:
                    hashCode ^= 0x80;
                    break;
                default:
					// TODO handle error
                    hashCode ^= 0x81;
                    break;
            }

            return hashCode;
        }

        public override string ToString()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("(");
            stringBuilder.Append(ToStringProperties());
            stringBuilder.Append(")");

            return stringBuilder.ToString();
        }

        internal override string ToStringProperties()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(base.ToStringProperties());
            stringBuilder.Append(", ");

            stringBuilder.Append("TelegramFunction=");
            switch (TelegramFunction)
            {
                case eTelegramFunction_Returning.CONFIRM_ACK:
                    stringBuilder.Append("CONFIRM_ACK");
                    break;
                case eTelegramFunction_Returning.CONFIRM_NAK:
                    stringBuilder.Append("CONFIRM_NAK");
                    break;
                case eTelegramFunction_Returning.FC_ERROR:
                    stringBuilder.Append("FC_ERROR");
                    break;
                case eTelegramFunction_Returning.RESPOND_NAK:
                    stringBuilder.Append("RESPOND_NAK");
                    break;
                case eTelegramFunction_Returning.RESPOND_STATUS:
                    stringBuilder.Append("RESPOND_STATUS");
                    break;
                case eTelegramFunction_Returning.RESPOND_NDAT:
                    stringBuilder.Append("RESPOND_NDAT");
                    break;
                default:
					// TODO handle error
                    stringBuilder.Append("UNKNOWN");
                    break;
            }

            stringBuilder.Append("', ");
            stringBuilder.Append("AccessDemandFlag='");
            stringBuilder.Append(AccessDemandFlag ? "true" : "false");
            stringBuilder.Append("'");

            stringBuilder.Append("', ");
            stringBuilder.Append("DataFlowControlStopFlag='");
            stringBuilder.Append(DataFlowControlStopFlag ? "true" : "false");
            stringBuilder.Append("'");
            return stringBuilder.ToString();
        }

        public override bool Equals(object obj)
        {
            Type objType = obj.GetType();
            if (this.GetType() == objType)
            {
                Telegram_Returning telegramReturning2Compare = obj as Telegram_Returning;
                return EqualsProperties(telegramReturning2Compare);
            }

            return false;
        }

        internal bool EqualsProperties(Telegram_Returning telegramReturning2Compare)
        {
            if (base.EqualsProperties(telegramReturning2Compare))
            {
                if (AccessDemandFlag == telegramReturning2Compare.AccessDemandFlag)
                {
                    if (DataFlowControlStopFlag == telegramReturning2Compare.DataFlowControlStopFlag)
                    {
                        return (TelegramFunction == telegramReturning2Compare.TelegramFunction);
                    }

                    return false;
                }

                return false;
            }

            return false;
		}

		#endregion ToString, Equals, GetHashCode
	}
}
